<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Queue extends Model
{
    //
    protected $table = 'queue';


    public static function start($union, $queue, $param = [])
    {
        $data = [
            'union' => $union,
            'queue' => $queue,
            'param' => json_encode($param, JSON_UNESCAPED_UNICODE),
            'start_time' => microtime(true)
        ];
        $id = self::insertGetId($data);
        return $id;
    }


    public static function end($id)
    {
        $queueItem = self::where('id', $id)->select('start_time')->first();
        if ($queueItem) {
            $end_time = microtime(true);
            $duration = $end_time - $queueItem->start_time;
            $duration = round($duration, 4);
            // $queueItem
            $data = [
                'end_time' => $end_time,
                'duration' => $duration
            ];
            self::where('id', $id)->update($data);
        }
    }
}
